from flask import request
from flask_classy import FlaskView, route

from ..schema.resp import AdditionalItemBase
from ..services.additional_item import AdditionalItemService
from ..services.currency import CurrencyService


class AdditionalItemsView(FlaskView):
    @route('/<item_id>')
    def fetch_by_id(self, item_id):
        item = AdditionalItemService.get_item_by_id(item_id)
        currency = request.args.get('currency_id', None)
        if not currency:
            currency = CurrencyService.get_main_currency()
        else:
            currency = CurrencyService.get_currency_by_id(currency)

        resp = AdditionalItemBase(context={'currency': currency})
        if item:
            item.currency = currency
        return resp.jsonify(item)
